//let url = `http://username:password@www.baidu.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument`;
function parseUrl(url,parseQuery=false){
   let pattern = /(?<protocal>https?|ftp|file):\/\/((?<username>[^:]+):(?<password>[^@]+)@)?(?<hostname>[^:\s?]+)(:(?<port>\d+))?(?<pathname>[^?\s]+)?(?<query>\?[^#]+)?(?<hash>#.+)?/;
   let result = url.match(pattern);
   const {protocal,username='',password='',hostname,port=80,pathname='/',query='',hash=''} = result.groups;
   let parsedResult =  {
    protocal,
    username,
    password,
    hostname,
    port,
    pathname,
    query,
    hash
   }
   if(parseQuery){
    let queryObj = {};//  query: '?name=zhufeng&age=16',
    query.slice(1).replace(/([^=&]+)=([^=&]+)/g,function(_,key,value){
        queryObj[key]=value;
    });
    parsedResult.query = queryObj;
   }
   return parsedResult;
}
let url1 = 'http://localhost?name=zhufeng&age=16';

let result = parseUrl(url1,true);
console.log(result);
